package iptux;

import java.awt.BorderLayout;
import java.awt.Component;
import java.io.File;
import java.util.Vector;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTabbedPane;

/**
 * File transmitting progress monitor.
 * @author cwll<cwll2009@126.com>
 *
 */
public class DialogFileTrans extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTable tableFileSend;
	private JTable tableFileReceive;
	JTabbedPane tabbedPane;
	Iptux mainDlg;

	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		try {
			DialogFileTrans dialog = new DialogFileTrans();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the dialog.
	 */
	public DialogFileTrans(Iptux mainDlg) {
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			tabbedPane = new JTabbedPane(JTabbedPane.TOP);
			contentPanel.add(tabbedPane, BorderLayout.CENTER);
			{
				tableFileSend = new JTable();
				JScrollPane scrollPane = new JScrollPane(tableFileSend);
				tabbedPane.addTab("File Send", null, scrollPane, null);
			}
			{
				tableFileReceive = new JTable();
				JScrollPane scrollPane = new JScrollPane(tableFileReceive);
				tabbedPane.addTab("File Receive", null, scrollPane, null);
			}
		}
		this.mainDlg = mainDlg;
		initialize();
	}
	
	public void initialize(){
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		Object[][] tableSendMode = new Object[1][5];
		String[] columnNames = {"palIP","filename", "size","progress", "full path"};
		DefaultTableModel sendModel = new DefaultTableModel(tableSendMode, columnNames);
		tableFileSend.setModel(sendModel);
		tableFileSend.getColumn("progress").setCellRenderer(new MyProgressBarRenderer());
		
		Object[][] tableReceiveMode = new Object[1][5];
		DefaultTableModel receiveModel = new DefaultTableModel(tableReceiveMode, columnNames);
		tableFileReceive.setModel(receiveModel);
		tableFileReceive.getColumn("progress").setCellRenderer(new MyProgressBarRenderer());
		
		ThreadFileTransStatus threadFileTransStatus = new ThreadFileTransStatus();
		Thread t1 = new Thread(threadFileTransStatus);
		t1.start();
	}
	
	/**
	 * Update the actived tabbedPane, show the file transmitting progress.
	 */
	public void updateFileTransStatus(){
		if (tabbedPane.getTitleAt(tabbedPane.getSelectedIndex()).equals("File Send")) {
			DefaultTableModel sendModel = (DefaultTableModel)tableFileSend.getModel();
			Vector dataVector =  sendModel.getDataVector();
			dataVector.clear();
			for (int index = 0; index < mainDlg.sendFileList.size(); index++) {
				Vector<Object> rowVector = new Vector<Object>();
				rowVector.add(mainDlg.sendFileList.get(index).fileown);
				int pathLength = mainDlg.sendFileList.get(index).filepath.split(File.separator).length;
				String fileName = mainDlg.sendFileList.get(index).filepath.split(File.separator)[pathLength-1];
				rowVector.add(fileName);
				rowVector.add(Utils.longSizeToUISize(mainDlg.sendFileList.get(index).filesize));
				rowVector.add(mainDlg.sendFileList.get(index).transmittedSize * 100
						/ mainDlg.sendFileList.get(index).filesize);
				rowVector.add(mainDlg.sendFileList.get(index).filepath);
				dataVector.add(rowVector);
			}
			tableFileSend.updateUI();
		} else {
			DefaultTableModel ReceiveMode = (DefaultTableModel)tableFileReceive.getModel();
			Vector dataVector =  ReceiveMode.getDataVector();
			dataVector.clear();
			for (int index = 0; index < mainDlg.rcvFileList.size(); index++) {
				Vector<Object> rowVector = new Vector<Object>();
				rowVector.add(mainDlg.rcvFileList.get(index).fileown);
				int pathLength = mainDlg.rcvFileList.get(index).filepath.split(File.separator).length;
				String fileName = mainDlg.rcvFileList.get(index).filepath.split(File.separator)[pathLength-1];
				rowVector.add(fileName);
				rowVector.add(Utils.longSizeToUISize(mainDlg.rcvFileList.get(index).filesize));
				rowVector.add(mainDlg.rcvFileList.get(index).transmittedSize * 100
						/ mainDlg.rcvFileList.get(index).filesize);
				rowVector.add(mainDlg.rcvFileList.get(index).filepath);
				dataVector.add(rowVector);
			}
			tableFileReceive.updateUI();
		}
	}
	
	/**
	 * Timer for update UI.
	 * @author cwll<cwll2009@126.com>
	 *
	 */
	class ThreadFileTransStatus implements Runnable{
		@Override
		public void run() {
			while(true){
            	try{
            	    Thread.currentThread().sleep(300);//暂停300毫秒秒后线程继续执行
            	    updateFileTransStatus();
            	}catch (InterruptedException e) {
            	    e.printStackTrace();
            	}
            }     
			
		}
		
	}
	
	/**
	 * Display a JProgressBar in JTable.
	 * @author cwll<cwll2009@126.com>
	 *
	 */
	class MyProgressBarRenderer extends JProgressBar implements TableCellRenderer{
		@Override
	    public Component getTableCellRendererComponent(JTable table, Object value,
	            boolean isSelected, boolean hasFocus, int row, int column) {
			if(value==null){
				return this;
			}
	        Integer v = Integer.valueOf(String.valueOf(value));//这一列必须都是integer类型(0-100)
	        setStringPainted(true);
	        setValue(v);
	        return this;
	    }
	}
}
